<?php

/**
 * Tools_SmsGatewayObject - Send SMS object
 *
 * @package
 * @author Alexis Eric
 * @copyright Copyright (c) 2012 - Erra Prod
 * @version $1.0$
 * @access public
 */
class Tools_SmsGatewayObject extends Erra_Fields_Base_Object {

    public static function getAccountInfos() {
        $url = 'http://www.envoyersmspro.com/api/account/getallaccounts';
        $login = Config_GeneralConfigObject::get('identifiant_sms');
        $password = Config_GeneralConfigObject::get('mot_de_passe_sms');
        $fields = array('login' => $login,
            'password' => $password);
        return post_curl($url, $fields);
    }

    public static function send($sendername, $recipients, $text) {
        ob_start();
        $url = 'http://www.envoyersmspro.com/api/message/send';
        $sendername = substr(strip_non_alnum($sendername), 0, 11);
        if (strlen($sendername) < 2) {
            $sendername .= 'agenda';
        }
        //login,password,sendername,recipients=0607080910,.....,text
        $finalRecipients = array();
        $recipients = str_replace(' ', '', $recipients);
        $recipients = strstr($recipients, ',') ? explode(',', $recipients) : array($recipients);
        foreach ($recipients as $recipient) {
            if (strlen($recipient) == 10 && is_numeric($recipient)) {
                $finalRecipients[] = '33' . substr($recipient, 1);
            }
        }
        $finalRecipients = implode(',', $finalRecipients);

        $login = Config_GeneralConfigObject::get('identifiant_sms');
        $password = Config_GeneralConfigObject::get('mot_de_passe_sms');
        $fields = array('login' => $login,
            'longmessageallowed' => 1,
            'password' => $password,
            'sendername' => $sendername,
            'recipients' => $finalRecipients,
            'text' => wysiwyg_to_html(br2nl($text)));
        $res = post_curl($url, $fields);
        $sms_contents = ob_get_contents();
        ob_end_clean();
        return 1;
    }

}

?>